import pyperclip

class TextProcessor:
    """用于处理多行文本，按指定规则为特定行添加序号和后缀"""

    DEFAULTS = {'n': 5, 'm': 3, 'y': 1}

    def __init__(self, n=5, m=3, x='', y=1, z='', o=''):
        self.n = int(n) if self._is_int(n) else 5
        self.m = int(m) if self._is_int(m) else 3
        self.x = str(x or '')
        self.y = int(y) if self._is_int(y) else 1
        self.z = str(z or '')
        self.o = str(o or '')

    # === Setter 函数 ===
    def set_n(self, n): self.n = int(n) if self._is_int(n) else self.DEFAULTS['n']
    def set_m(self, m): self.m = int(m) if self._is_int(m) else self.DEFAULTS['m']
    def set_x(self, x): self.x = str(x or '')
    def set_y(self, y): self.y = int(y) if self._is_int(y) else self.DEFAULTS['y']
    def set_z(self, z): self.z = str(z or '')
    def set_o(self, o): self.o = str(o or '')

    # === 工具函数 ===
    @staticmethod
    def _is_int(value):
        try:

            int(value)
            print(f"{value}是int")
            return True
        except (TypeError, ValueError):
            return False

    # === 主逻辑 ===
    def process_text(self, input_text):
        """处理输入的多行文本"""
        if not input_text:
            return ""

        if self.m > self.n:
            self.m = self.n

        lines = input_text.splitlines()
        result = []

        for i in range(0, len(lines), self.n):
            group = lines[i:i + self.n]
            for j, line in enumerate(group):
                if j == self.m - 1:
                    new_line = f"{self.x}{self.y}{self.z} {line} {self.o}".strip()
                    self.y += 1
                else:
                    new_line = line
                result.append(new_line)

        final_result = "\n".join(result)
        try:
            pyperclip.copy(final_result)
        except pyperclip.PyperclipException:
            print("⚠️ 无法复制到剪贴板，可能是系统不支持。")

        return final_result

    @staticmethod
    def read_input(end_marker="aaa") -> str:
        """从控制台读取多行文本，直到输入指定结束符"""
        print(f"请输入多行文本，以 '{end_marker}' 结束输入:")
        lines = []
        while True:
            try:
                line = input()
                if line == end_marker:
                    break
                lines.append(line)
            except EOFError:
                break
        return "\n".join(lines)


if __name__ == "__main__":
    input_text = TextProcessor.read_input()

    n = input("请输入每组行数 n（默认2）: ") or 2
    m = input("请输入第几行需要处理 m（默认1）: ") or 1
    x = input("请输入序号前缀 x（默认空）: ") or ''
    y = input("请输入起始序号 y（默认1）: ") or 1
    z = input("请输入序号后缀 z（默认'号'）: ") or '号'
    o = input("请输入每行末尾添加的字符串 o（默认'==='）: ") or '==='

    processor = TextProcessor(int(n), int(m), x, int(y), z, o)
    result = processor.process_text(input_text)

    print("\n处理后的文本:")
    print(result)
